<?php

	
	require dirname(dirname(__FILE__)).'/user-manage/functions.php';
	
	
	//获取文件后缀名函数
	function fileext($filename)
	{
		return substr(strrchr($filename, '.'), 1);
	}
	//生成随机文件名函数
	function random($length)
	{
		$hash = 'CR-';
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		mt_srand((double)microtime() * 1000000);
		for($i = 0; $i < $length; $i++)
		{
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}
	
	function jsAdvoid($str){
		$a ="/<script[^>]*?>.*?</script>/si";
		$keyword = preg_replace($a,' ',$str);
		return  $keyword;
	}
	
	function isGoodsnameExist($goodsname){
		
		
		$conn = get_db_conn();
		$sql = 'select count(*) as cnt from goods where name="'.$goodsname.'";';
		$result = mysql_query($sql,$conn) or die("数据库查询出错!");
		$ret = mysql_fetch_array($result);
		if($ret['cnt']>0)return true;
		else return false;
	}
	function isClassNameExist($className){
		$conn = get_db_conn();
		$sql = 'select count(*) as cnt from goods_class where name="'.$className.'";';
		$result = mysql_query($sql,$conn) or die("数据库查询出错!");
		$ret = mysql_fetch_array($result);
		if($ret['cnt']>0)return true;
		else return false;
		
		
	}
	
	
function addClass($params){
	$col="";
	$val="";
	foreach ($params as $key => $item) {
		$col.=",".$key;
		$val.=",".$item;
	}
	$sql = "insert into goods_class(".substr($col, 1).") values (".substr($val, 1).");";
	$conn = get_db_conn();
	if(mysql_query($sql,$conn))return true;
	else  {
		echo $sql;
		echo mysql_error();
		return false;
	}
	
}	

function addPhoto($params) {
	$col="";
	$val="";
	foreach ($params as $key => $item) {
		$col.=",".$key;
		$val.=",".$item;
	}
	$sql = "insert into photo(".substr($col, 1).") values (".substr($val, 1).");";
	$conn = get_db_conn();
	if(mysql_query($sql,$conn))return true;
	else  {
		echo $sql;
		echo mysql_error();
		return false;
	}
}

function getGoodsId($name){
	$_name=addslashes($name);
	$conn=get_db_conn();
	$sql='select id from goods where name="'.$_name.'";';
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$ret=mysql_fetch_array($result);
	mysql_close($conn);
	return dowith_sql($ret["id"]);
}

function getGoodsInfoByName($goodsName){
	$_goodsName=addslashes($goodsName);
	$conn=get_db_conn();
	$sql='select g.* ,p.store_name,p.url,p.id as pid,gc.name as class_name 
	from goods g left join photo p on g.id=p.goods_id 
	left join goods_class gc on g.goods_class_id=gc.id
	 where g.name="'.$_goodsName.'";';
	$result=mysql_query($sql,$conn) or die (mysql_error());
	$ret = mysql_fetch_array($result);
	mysql_close($conn);
	return $ret;	
		
}


function  getPhotoId($storeName){
	
	$_storeName=addslashes($storeName);
	$conn=get_db_conn();
	$sql='select id from photo where store_name="'.$_storeName.'";';
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$ret=mysql_fetch_array($result);
	mysql_close($conn);
	return dowith_sql($ret["id"]);
}


function  updatePhoto($params,$id){
	
	$sql = "update photo set ";
	$tmp = "";
	foreach ($params as $key => $item) {
		$tmp.=",".$key." = ".$item;
	}
	$sql = $sql.substr($tmp, 1).' where id = '.$id.';';
	$conn = get_db_conn();
	if(mysql_query($sql,$conn)) return true;
	else  return  false;
	
}

function getGoodsAndPhotoByClass($className){
	$_className=addslashes($className);
	$conn=get_db_conn();
	$sql='select g.*,p.url ,p.id as pid,p.store_name,gc.id as gc_id from goods_class gc left join 
			goods g on gc.id=g.goods_class_id left 
			join photo p on g.id=p.goods_id where gc.name="'.$_className.'";';
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$ret=array();
	$row_cnt=0;
	while($row = mysql_fetch_array($result)) {
		$ret[$row_cnt++]=array('name'=>$row['name'],
								'price'=>$row['price'],
								'piece_price'=>$row['piece_price'],
								'number'=>$row['number'],
								'goods_class_id'=>$row['goods_class_id'],
								'goods_describe'=>$row['goods_describe'],
								'url'=>$row['url'],
								'pid'=>$row['pid'],
								'id'=>$row['id'],
								'store_name'=>$row['store_name'],
								'gc_id'=>$row['gc_id'],
		);
	}
	mysql_close($conn);
	return $ret;


}
function addGoods($params) {
	$col="";
	$val="";
	foreach ($params as $key => $item) {
		$col.=",".$key;
		$val.=",".$item;
	}
	$sql = "insert into goods(".substr($col, 1).") values (".substr($val, 1).");";
	$conn = get_db_conn();
	if(mysql_query($sql,$conn))return true;
	else  {
		echo $sql;
		echo mysql_error();
		return false;
	}
}

	function getGoodsClassId($name){
		$_name=addslashes($name);
		$conn=get_db_conn();
		$sql='select id from goods_class where name="'.$_name.'";';
		$result = mysql_query($sql,$conn) or die(mysql_error());
		$ret=mysql_fetch_array($result);
		mysql_close($conn);
		return dowith_sql($ret["id"]);
		
		
	}
	
	
	
	function getGoodsClass(){
		$conn=get_db_conn();
		$sql="select * from goods_class order by id";
		$result = mysql_query($sql,$conn) or die(mysql_error());
		$ret=array();
		$row_cnt=0;
		while($row = mysql_fetch_array($result)) {
			$ret[$row_cnt++]=array('name'=>$row['name']);
		}
		mysql_close($conn);
		return $ret;
		
		
		
	}
	
	function updateGoodsClass($params,$id){
		$sql = "update goods_class set ";
		$tmp = "";
		foreach ($params as $key => $item) {
			$tmp.=",".$key." = ".$item;
		}
		$sql = $sql.substr($tmp, 1).' where id = '.$id.';';
		$conn = get_db_conn();
		if(mysql_query($sql,$conn)) return true;
		else  return false;

	
	}
	
	
	function updateGoods($params,$id) {
		$sql = "update goods set ";
		$tmp = "";
		foreach ($params as $key => $item) {
			$tmp.=",".$key." = ".$item;
		}
		$sql = $sql.substr($tmp, 1).' where id = '.$id.';';
		$conn = get_db_conn();
		if(mysql_query($sql,$conn)) return true;
		else  return false;
	}
	
	
	function deleteGoods($deleteId){
		$id = dowith_sql($deleteId);
		$id = addslashes($id);
		$conn = get_db_conn();
		$sql = "delete from goods where id =".$id.";";
		if(mysql_query($sql,$conn))return true;
		else return false;
		
	}
	
	function deletePhoto($deleteId){
		$id = dowith_sql($deleteId);
		$id = addslashes($id);
		$conn = get_db_conn();
		$sql = "delete from photo where id =".$id.";";
		if(mysql_query($sql,$conn))return true;
		else return false;
		
	}
	
	function deleteGoodsClass($deleteId){
		$id=dowith_sql($deleteId);
		$id=addslashes($id);
		$conn=get_db_conn();
		$sql="delete from goods_class where id=".$id.";";
		if (mysql_query($sql,$conn))return true;
		else return  false;
		
	}
	
	

?>